package com.neusoft.neuiotms.dcvm.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.neuiotms.dcvm.bean.LocationCiBean;
import com.neusoft.neuiotms.dcvm.service.LocationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("位置管理")
@RestController
@RequestMapping(value = "/api/w/locations")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @ApiOperation("获取园区（根节点）位置")
    @GetMapping("/root")
    public ResultDTO<?> rootLocation(HttpServletRequest request) {
        final Map<String, Object> res = this.locationService.getRootLocation(request);
        return ResultDTO.success(res);
    }

    @ApiOperation("根据父容器code获取子容器")
    @GetMapping("/{pCode}/deep/{deep}")
    public ResultListDTO<?> deepLocationsByOne(@PathVariable String pCode,
            @PathVariable Integer deep) {
        final List<LocationCiBean> res = this.locationService.getDeepLocationsByOne(pCode, deep);
        return ResultListDTO.success(res);
    }

    /**
     * 用户的角色编码，按照约定，就是位置编码。返回位置树时，就返回属于自己的位置
     *
     * @param request
     * @return
     */
    @ApiOperation("根据用户角色编码，获取对应的位置树")
    @GetMapping("/tree")
    public ResultListDTO<?> myLocation(UserBean user) {
        final List<LocationCiBean> res = this.locationService.getMyLocationsTree(user);
        return ResultListDTO.success(res);
    }

}
